package scau.hotel.hotel_service.validator.impl;


/**
 *
 * 正整数校验规则
 */

import org.springframework.util.StringUtils;
import scau.hotel.hotel_service.util.ValidatorUtil;
import scau.hotel.hotel_service.validator.IsPositiveInteger;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsPositiveIntegerValidator implements ConstraintValidator<IsPositiveInteger,String> {

    private boolean required = false;

    @Override
    public void initialize(IsPositiveInteger constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return ValidatorUtil.isPositiveInteger(s);
        }else{
            if(StringUtils.isEmpty(s)){
                return true;
            }else{
                return ValidatorUtil.isPositiveInteger(s);
            }
        }
    }
}
